<template>
  <a-drawer
    title="复试比例规则设置"
    width=500
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="rulesEditVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
         <a-col :sm="{span:12}"  :md="{span:12}">
              <a-form-item label='院系'>
                <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['ssyxm',{initialValue:postParams.ssyxm}]" @change="yxChange" disabled >
                      <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh +'-'+i.dwmc }}</a-select-option>
                    </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}"  :md="{span:12}">
              <a-form-item label='专业'>
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['sszym',{initialValue:postParams.sszym}]" >
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh +'-'+i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
          </a-col>
          <a-col :sm="{span:12}"  :md="{span:12}">
         <a-form-item label="学习形式">
           <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['qrzhfqrz',{initialValue:postParams.qrzhfqrz}]" >
             <a-select-option v-for="i in TYPE0441" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
           </a-select>
         </a-form-item>
       </a-col>
       <a-col :sm="{span:12}"  :md="{span:12}">
         <a-form-item label="复试人数">
              <a-input placeholder="请输入..."  v-decorator="['fsrs',{initialValue:postParams.fsrs}]" @keyup="rsChange"/>
            </a-form-item>
        </a-col>
          <a-col :sm="{span:12}"  :md="{span:12}">
           <a-form-item label="复试比例">
                <a-input placeholder="请输入..."  v-decorator="['sszybl',{initialValue:postParams.sszybl}]" @keyup="blChange"/>
              </a-form-item>
          </a-col>
       </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072','TYPE0441']
export default {
  props: {
    rulesEditVisible: {
      default: false
    },
    info: {
      default: () => ({})
    }
  },
  components: {
    STable
  },
  beforeCreate(){
    this.form = this.$form.createForm(this)
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
         bssjdd: '',
        lxfs:'',
        lxr:'',
        mssjdd:'',
        ssyxm:'',
        sszym:''
      },
      selectMenu: {
        yx: [] ,// 院系
        zy:[]
      }
    }
  },
  watch: {
    info () {
      this.form.resetFields();
      this.postParams = this.info
      this.getProssion(this.postParams.ssyxm)
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0441: state => state.app['TYPE0441']
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    reset () {
    },
      // 选择院系
    yxChange (code) {
      this.$set(this.postParams, 'sszym', '')
      this.$set(this.selectMenu, 'zy', [])
      if(code){
        this.getProssion(code)
      }
    },
   rsChange(){
      let params = {
       fsrs:this.form.getFieldValue('fsrs'),
       ssyxm:this.form.getFieldValue('ssyxm'),
       sszym:this.form.getFieldValue('sszym'),
       qrzhfqrz:this.form.getFieldValue('qrzhfqrz')
     }
     this.$api.base.getProportion(params).then(res =>{
       this.form.setFieldsValue({'sszybl':res.data})
     })
   },
    blChange(){
      let params = {
        sszybl:this.form.getFieldValue('sszybl'),
        ssyxm:this.form.getFieldValue('ssyxm'),
        sszym:this.form.getFieldValue('sszym'),
        qrzhfqrz:this.form.getFieldValue('qrzhfqrz')
      }
      this.$api.base.getProportion(params).then(res =>{
        this.form.setFieldsValue({'fsrs':res.data})
      })
    },
    getProssion(code){
       this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 选择招生年度
    zsndChange () {

    },
    // 关闭
    onClose () {
      this.form.resetFields();
      this.$emit('close')
    },
    // 保存
    handleSubmit () {
      this.form.validateFields((err,values) => {
        if(err){
          this.$message.error('请选择必填项')
        }else{
          var params = Object.assign({id:this.postParams.id,zsxn:this.postParams.zsxn},this.form.getFieldsValue())
          this.$api.recruitstat.reExamProRuleSetUpdate(params).then( res => {
             this.reset()
            this.$emit('success')
          })
        }
      })
    }
  }
}
</script>
